package com.enjoylearning.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.enjoylearning.auth.service.RegisterService;
import com.enjoylearning.base.exception.XueChengPlusException;
import com.enjoylearning.base.model.RestResponse;
import com.enjoylearning.base.utils.SecurityUtil;
import com.enjoylearning.ucenter.feignclient.CheckCodeClient;
import com.enjoylearning.ucenter.mapper.XcUserMapper;
import com.enjoylearning.ucenter.mapper.XcUserRoleMapper;
import com.enjoylearning.ucenter.model.dto.RegisterParamsDto;
import com.enjoylearning.ucenter.model.po.XcUser;
import com.enjoylearning.ucenter.model.po.XcUserRole;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;

@Service
public class RegisterServiceImpl implements RegisterService {

    @Autowired
    private CheckCodeClient checkCodeClient;
    @Autowired
    private XcUserMapper xcUserMapper;
    @Autowired
    private XcUserRoleMapper xcUserRoleMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public RestResponse register(RegisterParamsDto registerParamsDto) {
        //1、校验验证码，如果不一致则抛出异常
        String checkcodekey = registerParamsDto.getCheckcodekey();
        String checkcode = registerParamsDto.getCheckcode();
        Boolean verify = checkCodeClient.verify(checkcodekey, checkcode);
        if (!verify) {
            XueChengPlusException.cast("验证码错误");
        }
        //2、校验两次密码是否一致，如果不一致则抛出异常
        String password = registerParamsDto.getPassword();
        String confirmpwd = registerParamsDto.getConfirmpwd();
        if (!password.equals(confirmpwd)) {
            XueChengPlusException.cast("密码不一致");
        }
        //3、校验用户是否存在，如果存在则抛出异常
        String cellphone = registerParamsDto.getCellphone();
        XcUser xcUser = xcUserMapper.selectOne(new LambdaQueryWrapper<XcUser>().eq(XcUser::getCellphone, cellphone));
        if (xcUser != null) {
            XueChengPlusException.cast("用户已注册");
        }
        //4、向用户表、用户角色关系表添加数据。角色为学生角色。
        XcUser newXcUser = new XcUser();
        BeanUtils.copyProperties(registerParamsDto, newXcUser);
        newXcUser.setStatus("1");
        newXcUser.setCreateTime(LocalDateTime.now());
        newXcUser.setPassword(passwordEncoder.encode(password));
        newXcUser.setName(registerParamsDto.getNickname());
        newXcUser.setUtype("101001");
        xcUserMapper.insert(newXcUser);
        XcUserRole xcUserRole = new XcUserRole();
        xcUserRole.setUserId(newXcUser.getId());
        xcUserRole.setRoleId("17");
        xcUserRole.setCreateTime(LocalDateTime.now());
        xcUserRoleMapper.insert(xcUserRole);
        return RestResponse.success();
    }
}
